<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改高峰论坛')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-content-edit" th:object="${cmsContent}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">标题：</label>
            <div class="col-sm-8">
                <textarea name="title" class="form-control" placeholder="关于我们标题">[[*{title}]]</textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">内容：</label>
            <div class="col-sm-8">
                <input type="hidden" class="form-control" th:field="*{content}">
                <div class="summernote" id="content"></div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">排序：</label>
            <div class="col-sm-8">
                <input name="idx" th:field="*{idx}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">跳转地址：</label>
            <div class="col-sm-8">
                <input name="ext2" class="form-control" type="text"  th:field="*{ext2}" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">预览图</label>
            <input type="hidden" name="ext1" id="ext1" th:field="*{ext1}"/>
            <div class="col-sm-8">
                <input id="singleFileExt" name="file" type="file">
            </div>
        </div>
        <div class="form-group">
            <input name="thumb" id="thumb" th:field="*{thumb}" class="form-control" type="hidden">
            <label class="col-sm-3 control-label ">背景图</label>
            <div class="col-sm-8">
                <input id="singleFile" name="file" type="file">
            </div>
        </div>

        <div class="box-footer">
            <div class="col-sm-offset-5 col-sm-6">
                <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
                <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: summernote-js" />
<th:block th:include="include :: bootstrap-fileinput-js" />
<script th:inline="javascript">
    var prefix = ctx + "news/content";
    var imgUrl =  [[${cmsContent.thumb}]]
    var ext1 =  [[${cmsContent.ext1}]]

    var initialPreview = []
    var initialPreviewExt = []
    if(imgUrl) {
        initialPreview = [base+imgUrl]
    }
    if(ext1) {
        initialPreviewExt = [base + ext1]
    }
    $("#form-content-edit").validate({
        rules: {
            title: "required",
            idx: "required"
        },
        messages: {
            title:  "必填项",
            idx:  "必填项"
        }
    });


    $('#singleFileExt').fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true,
        initialPreviewAsData: true,
        initialPreview: initialPreviewExt
    }).on('fileuploaded', function (event, data, previewId, index) {
        var rsp = data.response;
        log.info("return url：" + rsp.url)

        $('#ext1').val(rsp.fileName)
    }).on('fileremoved', function (event, id, index) {
        $('#ext1').val('')
        $("input[name='" + event.currentTarget.id + "']").val('')
    }).on('filedeleted', function() {
        $('#ext1').val('')
    }).on('filesuccessremove', function() {
        $('#ext1').val('')
    }).on('filecleared', function() {
        $('#ext1').val('')
    })

    $("#singleFile").fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true,
        initialPreviewAsData: true,
        initialPreview: initialPreview
    }).on('fileuploaded', function (event, data, previewId, index) {
        var rsp = data.response;
        log.info("return url：" + rsp.url)
        log.info("reutrn fileName：" + rsp.fileName)
        log.info("reutrn newFileName：" + rsp.newFileName)
        log.info("return originalFilename：" + rsp.originalFilename)

        $('#thumb').val(rsp.fileName)
    }).on('fileremoved', function (event, id, index) {
        $('#thumb').val('')
        $("input[name='" + event.currentTarget.id + "']").val('')
    }).on('filedeleted', function() {
        $('#thumb').val('')
    }).on('filesuccessremove', function() {
        $('#thumb').val('')
    }).on('filecleared', function() {
        $('#thumb').val('')
    })

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.saveTab(prefix + "/edit", $('#form-content-edit').serialize());
        }
    }

    $(function() {
        $('.summernote').each(function(i) {
            $('#' + this.id).summernote({
                lang: 'zh-CN',
                dialogsInBody: true,
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                        var data = new FormData();
                        data.append("file", files[0]);
                        $.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                            data: data,
                            cache: false,
                            contentType: false,
                            processData: false,
                            dataType: 'json',
                            success: function(result) {
                                if (result.code == web_status.SUCCESS) {
                                    $('#' + obj.id).summernote('insertImage', result.url);
                                } else {
                                    $.modal.alertError(result.msg);
                                }
                            },
                            error: function(error) {
                                $.modal.alertWarning("图片上传失败。");
                            }
                        });
                    }
                }
            });
            var content = $("input[name='" + this.id + "']").val();
            $('#' + this.id).summernote('code', content);
        })
    });
</script>
</body>
</html>